package com.xingkeduo.util.pojo.base;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;

public class BaseJson {
	protected static ObjectMapper MAPPER = new ObjectMapper();

	protected static void toBaseJson(ObjectNode root, BaseTable<?> table) {
		SimpleDateFormat formatTimestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		if (table != null) {
			root.put("id", String.valueOf(table.getId()));
			root.put("code", String.valueOf(table.getCode()));
			root.put("createdby", table.getCreatedBy());
			if (table.getCreatedTime() != null)
				root.put("createdtime", formatTimestamp.format(table.getCreatedTime()));
			root.put("updatedby", table.getUpdatedBy());
			if (table.getUpdatedTime() != null)
				root.put("updatedtime", formatTimestamp.format(table.getUpdatedTime()));
		}
	}

	protected static JsonNode validJson(String json) throws Throwable {
		if (json.isEmpty())
			return null;

		JsonNode root = MAPPER.readTree(json);
		if (!root.isObject())
			throw new InputParamException(InputParamException.Type.INVALID, "参数格式不正确");
		return root;
	}

	protected static long validId(JsonNode node, YesNoEnum isRequired) throws Throwable {
		return validPrimary(node, "id", isRequired);
	}

	/**
	 * 验证主键类属性
	 * 
	 * @author GJ
	 * @time 2019年3月18日 上午11:21:09
	 * @param node
	 *            JsonNode对象
	 * @param key
	 *            json的key值
	 * @param isRequired
	 *            是否必须
	 * @return
	 * @throws Throwable
	 */
	protected static long validPrimary(JsonNode node, String key, YesNoEnum isRequired) throws Throwable {
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isPositiveInteger(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return Long.parseLong(value);
		}
		return 0;
	}

	/**
	 * 验证字符串属性
	 * 
	 * @author GJ
	 * @time 2019年3月18日 上午11:25:11
	 * @param node
	 *            JsonNode对象
	 * @param key
	 *            json的key值
	 * @param max
	 *            最大长度
	 * @param isRequired
	 *            是否必须
	 * @return
	 * @throws Throwable
	 */
	protected static String validString(JsonNode node, String key, int max, YesNoEnum isRequired) throws Throwable {
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (CommonUtil.isEmpty(value) || value.length() > max)
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return value;
		}
		return null;
	}

	public static long validCode(JsonNode node, YesNoEnum isRequired) {
		String key = "code";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (CommonUtil.isEmpty(value) || value.length() > 30)
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return Long.parseLong(value);
		}
		return 0;
	}

	protected static Date validTime(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "time";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isPositiveInteger(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return new Date(Long.parseLong(value));
		}
		return null;
	}

	protected static Date validTimeStart(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "start";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isPositiveInteger(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return new Date(Long.parseLong(value));
		}
		return null;
	}

	protected static Date validTimeEnd(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "end";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isPositiveInteger(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return new Date(Long.parseLong(value));
		}
		return null;
	}

	protected static Date validPage(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "page";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isPositiveInteger(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return new Date(Long.parseLong(value));
		}
		return null;
	}

	protected static int validPageIndex(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "index";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isPositiveInteger(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return Integer.parseInt(value);
		}
		return 0;
	}

	protected static int validPageSize(JsonNode node, YesNoEnum isRequired) throws Throwable {
		String key = "size";
		if (YesNoEnum.YES == isRequired && !node.has(key))
			throw new InputParamException(InputParamException.Type.NULL, key);

		if (node.has(key)) {
			String value = node.path(key).asText().trim();
			if (!RegularUtil.isPositiveInteger(value))
				throw new InputParamException(InputParamException.Type.INVALID, key + " error");
			return Integer.parseInt(value);
		}
		return 0;
	}
}
